$(document).ready(function() {
	mui.init();
	mui.plusReady(function() {
		Bluetooth();
		//点击确定按钮
		mui("body").on("tap", "#submit", function() {
			$("#loginPassworld, #newPassworld, #confirm").blur();
			//如果页面数据没有缺少
			if($('#loginPassworld').val() && $('#newPassworld').val() && $('#confirm').val()) {
				
				//如果新密码等于确认密码
				if($("#newPassworld").val() == $("#confirm").val()) {
					plus.nativeUI.showWaiting("正在操作中，请勿退出...");
					require(weburl + modelRepasswd,"json","post",{
						token: localStorage.getItem("token"),
						phone: localStorage.getItem("USERID"),
						code: $("#loginPassworld").val(),
						pay_password: $('#newPassworld').val(),
						pay_password_confirmation: $('#confirm').val()
					},function(data){
						plus.nativeUI.closeWaiting();
						mui.alert(data.msg, " ", "确定", function(e) {
							$('#loginPassworld').val("");
							$('#newPassworld').val("");
							$('#confirm').val("");
							$('.imgcode input').val('');
							$('input').blur();
						}, "div");
					},function(){
						mui.alert("系统繁忙，请稍后重试"," ","确定",function(){},"div");
					})
					
				} else {  //如果确认密码不等于新密码
					mui.alert("新密码和确认新密码不相同", " ", "确定", function() {}, "div");
				}

			} else {
				if(!$("#loginPassworld").val()) {
					mui.alert("请输入短信验证码", " ", "确定", function() {}, "div");
				} else if(!$("#newPassworld").val()) {
					mui.alert("请输入新密码", " ", "确定", function() {}, "div");
				} else if(!$('#confirm').val()) {
					mui.alert("请确认新密码", " ", "确定", function() {}, "div");
				}
			}
		})
	})

	//点击显示密码按钮
	mui("body").on("tap", "#bloc", function() {
		$("#loginPassworld, #newPassworld, #confirm").blur();
		if($(this).children(".bloc").attr("data-sign") == "false") {
			$(this).children(".bloc").attr("data-sign", "true");
			$(this).children(".bloc").css("background-color", "#000");
			$("#loginPassworld, #newPassworld, #confirm").attr("type", "text");
		} else {
			$(this).children(".bloc").attr("data-sign", "false");
			$(this).children(".bloc").css("background-color", "white");
			$("#loginPassworld, #newPassworld, #confirm").attr("type", "password");
		}
	})
	var signTime=60;
	// 点击图形验证码
	var imgCodeTime = false;
	$('.imgcode img').attr('src', weburl + Captcha);
	mui('body').on('tap', '.imgcode img', function () {
		if (imgCodeTime == true) {
			return false;
		}
		$(this).attr('src', weburl + Captcha +'?id=' + Math.random()*100);
		imgCodeTime = true;
		setTimeout(function(){
			imgCodeTime = false;
		}, 3000)
	})
	
	//点击发送验证码
	$("#yanzhenma").click(function() {
		if ($('.imgcode input').val() == '') {
			mui.alert("请输入图形验证码"," ","确定",function(){},"div");
			return false;
		}
		mui.confirm("是否给" + localStorage.getItem("USERID") + "发送验证码？", " ", ["发送", "取消"], function(e) {
			if(e.index == 0) {
				$("#yanzhenma").attr("disabled","disabled");
				console.log($('.imgcode input').val());
				plus.nativeUI.showWaiting("短信发送中，请勿退出...");
				data = {
					token: localStorage.getItem("token"),
					phone: localStorage.getItem("USERID"),
					validate: $('.imgcode input').val()
				}
				mui.ajax(weburl + model_sendY, {
					data: data,
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 500000, //超时时间设置为10秒；
					headers: {
						'Content-Type': 'application/json'
					},
					success: function(response) {
						plus.nativeUI.closeWaiting();
						if(response.status == "success") {
							mui.alert("验证码已发送，请注意查收", " ", "确定", function() {}, "div");
							var setTra=setInterval(function(){
								if(signTime>0){
									$("#yanzhenma").html(signTime+"s");
									signTime--;
									return false;
								}
								clearInterval(setTra)
								$("#yanzhenma").html("验证码");
								signTime=60;
								$("#yanzhenma").removeAttr("disabled");
							},1000)
						} else {
							$("#yanzhenma").html("验证码");
							$("#yanzhenma").removeAttr("disabled");
							mui.alert(response.msg, " ", "确定", function() {}, "div");
						}

					},
					error: function(xhr, type, errorThrown) {
						plus.nativeUI.closeWaiting();
						//异常处理；
						$("#yanzhenma").html("验证码");
						$("#yanzhenma").removeAttr("disabled");
						mui.alert("验证码发送失败，请稍后再试", " ", "确定", function() {}, "div");
					}
				});
			}
		}, "div");
	})

})